package com.ruoyi.quality.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.quality.domain.InspectionTemplate;
import com.ruoyi.quality.domain.InspectionTemplateItem;
import com.ruoyi.quality.domain.SamplingItem;
import com.ruoyi.quality.domain.SamplingItem;
import com.ruoyi.quality.domain.dto.SamplingItemDto;
import com.ruoyi.quality.domain.vo.SamplingItemVo;
import com.ruoyi.quality.domain.vo.SamplingItemVo;
import com.ruoyi.quality.mapper.SamplingItemMapper;
import com.ruoyi.quality.service.SamplingItemService;
import com.ruoyi.quality.mapper.SamplingItemMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

/**
 *
 */
@Service
public class SamplingItemServiceImpl implements SamplingItemService{

    @Resource
    SamplingItemMapper samplingItemMapper;

    @Override
    public Page<SamplingItemVo> getSamplingItemByPage(SamplingItemDto pageEntity) {
        MPJLambdaWrapper<SamplingItem> wrapper = new MPJLambdaWrapper<SamplingItem>()
                .selectAll(SamplingItem.class)//查询user表全部字段
                //记的测试阶段
                .eq(SamplingItem::getDelFlag, 0)
                .eq(null != pageEntity.getSamplingId(), SamplingItem::getSamplingId,pageEntity.getSamplingId())
                .orderByDesc(SamplingItem::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, SamplingItem.class, pageEntity);
        Page<SamplingItemVo> userList = samplingItemMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()), SamplingItemVo.class, wrapper);
        return userList;
    }

    @Override
    public void addSamplingItem(SamplingItemDto testDto) throws ParseException {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<SamplingItem> wrapper = new MPJLambdaWrapper<SamplingItem>()
                .eq(SamplingItem::getSamplingId, testDto.getSamplingId())
                .eq(SamplingItem::getCode, testDto.getCode())
                .ne(SamplingItem::getDelFlag, 1);
        SamplingItem material = samplingItemMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        SamplingItem test2 = new SamplingItem();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setSamplingId(testDto.getSamplingId());
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        samplingItemMapper.insert(test2);
    }

    @Override
    public void updateSamplingItem(SamplingItemDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<SamplingItem> wrapper = new MPJLambdaWrapper<SamplingItem>()
                .eq(SamplingItem::getSamplingId, testDto.getSamplingId())
                .eq(SamplingItem::getCode, testDto.getCode())
                .ne(SamplingItem::getDelFlag, 1);
                ;
        SamplingItem samplingItem = samplingItemMapper.selectOne(wrapper);
        if(null == samplingItem) {
            throw new GlobalException("该SamplingItem不存在");
        }
        BeanUtils.copyProperties(testDto, samplingItem, new String[]{"id"});
        samplingItem.setUpdateBy(userName);
        samplingItem.setUpdateTime(new Date());
        samplingItemMapper.updateById(samplingItem);
    }

    @Override
    public void delSamplingItem(SamplingItemDto testDto) {
        SamplingItem material = samplingItemMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该SamplingItem不存在");
        }
        material.setDelFlag(true);
        samplingItemMapper.updateById(material);
    }
}




